from tests.HTMLTestRunner import HTMLTestRunner

class EdenHTMLTestRunner(HTMLTestRunner):

    def _generate_report_test(self, rows, cid, tid, n, t, o, e):
        # e.g. "pt1.1", "ft1.1", etc
        has_output = bool(o or e)
        tid = (n == 0 and "p" or "f") + "t%s.%s" % (cid+1, tid+1)
        name = t.id().split(".")[-1]
        doc = t.shortDescription() or ""
        desc = doc and ("%s: %s" % (name, doc)) or name
        tmpl = has_output and self.REPORT_TEST_WITH_OUTPUT_TMPL or self.REPORT_TEST_NO_OUTPUT_TMPL

        # o and e should be byte string because they are collected from stdout and stderr?
        if isinstance(o, str):
            # TODO: some problem with "string_escape": it escapes \n and messes up formating
            # uo = unicode(o.encode("string_escape"))
            uo = o.decode("latin-1")
        else:
            uo = o
        if isinstance(e, str):
            # TODO: some problem with "string_escape": it escapes \n and messes up formating
            # ue = unicode(e.encode("string_escape"))
            ue = e.decode("latin-1")
        else:
            ue = e

        script = self.REPORT_TEST_OUTPUT_TMPL % dict(
            id = tid,
            output = uo + ue,
        )

        row = tmpl % dict(
            tid = tid,
            Class = (n == 0 and "hiddenRow" or "none"),
            style = n == 2 and "errorCase" or (n == 1 and "failCase" or "none"),
            desc = desc,
            script = script,
            status = self.STATUS[n],
        )
        rows.append(row)
        if not has_output:
            return
